//! @author Adam Emil Skoog
//! @date   2012-01-05

#include "Mehdi.h"
#include "Game.h"
#include "Level.h"
#include <cmath>

namespace mehdi
 {
	Mehdi::Mehdi()
	:
	view          (Game::getWindow()->GetDefaultView()),
	mayJump       (false),
	hasPressedDown(false)
	 {
		if (!image)
		 {
			image       = Game::getImageLoader()->get("mehdi");
			imageTopBar = Game::getImageLoader()->get("topBar");
			imageLetter = Game::getImageLoader()->get("letter");

			imageTopBar->SetColor(sf::Color(255,255,255,180));
		 }

		keyReceiver.registerOnKeyPress(onKeyPress);
		keyReceiver.registerOnKeyRelease(onKeyRelease);
		currentMehdi = this;

		CollisionMask *const mask(createCollisionMask());
		mask->setSize(sf::Vector2f(image->getImage()->GetWidth(),
		                           image->getImage()->GetHeight()));

		Game::getWindow()->SetView(view);
	 }

	Mehdi *const Mehdi::getCurrentMehdi()
	 {
		return currentMehdi;
	 }

	void Mehdi::drawHUD()
	 {
		imageTopBar->SetPosition(Game::getWindow()->GetView().GetRect().Left + 10.f,
								 Game::getWindow()->GetView().GetRect().Top + 10.f);
		imageLetter->SetPosition(imageTopBar->GetPosition());
		Game::getWindow()->Draw(*imageTopBar);
		Game::getWindow()->Draw(*imageLetter);
	 }

	void Mehdi::draw() const
	 {
		if (getHorizontalSpeed() != .0f)
			image->SetScale(1.f,1.f + std::sin(Game::getPlaytime() * 20.f) * .01f);
		else
			image->SetScale(1.f,1.f);		 

		image->SetPosition(position());
		Game::getWindow()->Draw(*image);
	 }
	
	void Mehdi::update()
	 {
		if (position().x > Game::getWindow()->GetWidth() / 2.f - getCollisionMasks()[0]->getSize().x / 2.f && Game::getLevel()->cameraMovementIsEnabled())
			view.SetCenter(position().x + getCollisionMasks()[0]->getSize().x / 2.f,view.GetCenter().y);
		else
		 {
			view.SetCenter(Game::getWindow()->GetWidth() / 2.f,view.GetCenter().y);

			if (position().x > Game::getWindow()->GetWidth() - getCollisionMasks()[0]->getSize().x)
				position().x = Game::getWindow()->GetWidth() - getCollisionMasks()[0]->getSize().x;
		 }

		if (position().x < .0f)
			position().x = .0f;

		hasPressedDown = false;
	 }

	const bool &Mehdi::pressedDown() const
	 {
		return hasPressedDown;
	 }

	void Mehdi::onLand()
	 {
		mayJump = true;
	 }

	void Mehdi::onKeyPress(const sf::Key::Code &key)
	 {
		if (key == sf::Key::Left)
		 {
			currentMehdi->setHorizontalSpeed(-200.f);
			currentMehdi->image->FlipX(true);
		 }

		if (key == sf::Key::Right)
		 {
			currentMehdi->setHorizontalSpeed(200.f);
			currentMehdi->image->FlipX(false);
		 }

		if (key == sf::Key::Up && currentMehdi->mayJump)
		 {
			currentMehdi->mayJump = false;
			currentMehdi->setVerticalSpeed(-.55f);
			currentMehdi->position().y -= 1.f;
		 }

		if (key == sf::Key::Down)
			currentMehdi->hasPressedDown = true;
	 }

	void Mehdi::onKeyRelease(const sf::Key::Code &key)
	 {
		if (key == sf::Key::Left)
			if (!kedja::Component::getEventManager()->keyIsDown(sf::Key::Right))
				currentMehdi->setHorizontalSpeed(.0f);

		if (key == sf::Key::Right)
			if (!kedja::Component::getEventManager()->keyIsDown(sf::Key::Left))
				currentMehdi->setHorizontalSpeed(.0f);
	 }

	Image *Mehdi::image        = NULL,
		  *Mehdi::imageTopBar  = NULL,
		  *Mehdi::imageLetter  = NULL;
	Mehdi *Mehdi::currentMehdi = NULL;
 }
